﻿using System;
using System.Web;
using System.Net.Mail;
using System.Configuration;
using System.IO;

namespace Framework.Util.EmailUtil
{
    public class SendMail
    {        
        private static SendMail _instant;

        public string AdminString
        {
            get { return "administrator"; }
        }

        public static SendMail GetInstant()
        {
            return _instant ?? (_instant = new SendMail());
        }

        public  SendMail()
        {

        }

        public bool SendEmail(string toname, string toaddress, string subject, string content)
        {
            var smtpserver =ConfigurationManager.AppSettings["Smtp_Server"];
            var smtpport = int.Parse(ConfigurationManager.AppSettings["Smtp_Port"]);
            var smtpssl = bool.Parse(ConfigurationManager.AppSettings["Smtp_SSL"]);
            var fromaddress = ConfigurationManager.AppSettings["Smtp_FromEmailAddress"];
            const string fromname = "suport@gamebetplus.com";
            var fromusername = ConfigurationManager.AppSettings["Smtp_FromEmailUsername"];
            var frompassword = ConfigurationManager.AppSettings["Smtp_FromEmailPassword"];
            
            var msg = new MailMessage(fromname, toaddress, subject, content)
                          {
                              IsBodyHtml = true
                          };
            var smtp = new SmtpClient
                           {
                               Host = smtpserver,
                               Port = smtpport,
                               Credentials = new System.Net.NetworkCredential(fromusername, frompassword)
                           };


            smtp.Send(msg);
            return true;
        }

        public DateTime GetFirstDayOfMonth(DateTime dtDate)
        {

            var dtFrom = dtDate;
            dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));
            return dtFrom;

        }

        public DateTime GetFirstDayOfMonth(int iMonth)
        {
            var dtFrom = new DateTime(DateTime.Now.Year, iMonth, 1);
            dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));
            return dtFrom;
        }

        public DateTime GetLastDayOfMonth(DateTime dtDate)
        {
            var dtTo = dtDate;
            dtTo = dtTo.AddMonths(1);
            dtTo = dtTo.AddDays(-(dtTo.Day));
            return dtTo;
        }

        public DateTime GetLastDayOfMonth(int iMonth)
        {
            var dtTo = new DateTime(DateTime.Now.Year, iMonth, 1);
            dtTo = dtTo.AddMonths(1);
            dtTo = dtTo.AddDays(-(dtTo.Day));
            return dtTo;
        }
                
        private  string FilePath
        {
            get
            {
                string filePath = HttpContext.Current.Server.MapPath("~/Content/Report");
                if (!Directory.Exists(filePath))
                    Directory.CreateDirectory(filePath);
                if (!filePath.EndsWith("\\"))
                    filePath += "\\";

                return filePath + FileName;
            }
        }
        private  string FileName
        {
            get;
            set;
        }
        
    }

    public class EmailEntity
    {
        public string UserName{ get; set; }
        public string FulName { get; set; }
        public string Subject { get; set; }
        public string Title { get; set; }        
        public string Password { get; set; }        
    }
}